package modelLayer;
import java.util.ArrayList;
public class LoanContainer
{
    private static LoanContainer instance;      
    private ArrayList<Loan> loans;
    /**
     * Constructor for objects of class LoanContainer
     */
    private LoanContainer()
    {
        loans = new ArrayList<Loan>();
        
    }
    public static LoanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new LoanContainer();
        }
        return instance;
    }
    public void addLoan(Loan loan)
    {
        loans.add(loan);
    }
    public ArrayList<Loan> getList()
    {
        return loans;
    }  
    
    public void findLoan(String Phone)
    {
        Loan loan = getLoan(Phone);
        if(loan !=null)
        {
                System.out.println("borrowDate " + loan.getborrowDate());
                System.out.println("period " + loan.getperiod());
                System.out.println("state" + loan.getstate());
            }
        
    }
    public Loan getLoan(String Phone)
    {
           boolean found = false;
        for(int i = 0; i < loans.size(); i++)
        {
            if(loans.get(i).getloaner().getPerson("phone").equals(Phone))
            {
                found = true;
                return loans.get(i);                
            }
        }
        if(!found)
        {
            System.out.println("person ikke fundet");
        }
        return null;
    }
    public void deleteLoan(String phone)
    {
        Loan loan = getLoan(phone);
        loans.remove(loan);
    }
    
    public void createLoan(int id, String borrowDate,String period,String state,Person loaner, DVD dvd)
    {     
            Loan loan = new Loan(id, borrowDate, period, state, loaner, dvd);
            addLoan(loan);            
            dvd.getCopys().remove(dvd.getCopys().size() - 1);
            System.out.println("Loan added");       
    }
    
   public void updateLoan(String phone, String period)
  { 
        Loan loan = getLoan(phone);
        loan.setperiod(period);      
  }
}
